package com.enba.boot.mq.sender.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.enba.boot.mq.MessageInteface;
import com.enba.boot.mq.MessageSenderStrategy;
import com.enba.boot.mq.properties.MqProperties;
import com.enba.boot.mq.sender.activemq.ActiveMqMessageBase;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/** Rabbitmq 集群模式消息发送 */
public class RabbitmqMessageSenderStrategy implements MessageSenderStrategy<ActiveMqMessageBase> {

  @Autowired private RabbitTemplate rabbitTemplate;

  @Autowired private MqProperties mqProperties;

  @Override
  public String getStrategyKey() {
    return mqProperties.getRabbitmq().getStrategyKey();
  }

  @Override
  public void sendMessageByQueue(String routingKey, String message) {
    logger.info(
        "RabbitmqClusterMessageSenderStrategy.sendMessageByQueue routingKey:{}, message:{}",
        routingKey,
        message);

    rabbitTemplate.convertAndSend(routingKey, message);
  }

  @Override
  public void sendMessageByTopic(String exchange, String message) {
    logger.info(
        "RabbitmqClusterMessageSenderStrategy.sendMessageByTopic exchange:{}, message:{}",
        exchange,
        message);

    rabbitTemplate.convertAndSend(exchange, "", message);
  }

  @Override
  public void sendMessageByTopic(String topicName, String message, String key) {

  }

  @Override
  public void sendMessageByQueue(
      String routingKey, MessageInteface<ActiveMqMessageBase> messageInteface) {
    logger.info(
        "RabbitmqClusterMessageSenderStrategy.sendMessageByQueue routingKey:{}, messageInteface:{}",
        routingKey,
        JSON.toJSONString(messageInteface));

    rabbitTemplate.convertAndSend(routingKey, messageInteface);
  }

  @Override
  public void sendMessageByTopic(
      String exchange, MessageInteface<ActiveMqMessageBase> messageInteface) {
    logger.info(
        "RabbitmqClusterMessageSenderStrategy.sendMessageByTopic exchange:{}, message:{}",
        exchange,
        JSON.toJSONString(messageInteface));

    rabbitTemplate.convertAndSend(exchange, "", messageInteface);
  }
}
